﻿using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.Linq;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;
using System.Collections.Generic;
using Microsoft.SharePoint.Workflow;
using System.Xml.Serialization;
using System.Xml;

namespace Moss.Workflow.Activities
{
    public sealed partial class CreateMultipleTasks : SequentialWorkflowActivity
    {
        #region CTOR
        public CreateMultipleTasks()
        {
            InitializeComponent();
        }
        #endregion

        #region Workflow related variables
        public SPWorkflowActivationProperties WorkflowProperties = new Microsoft.SharePoint.Workflow.SPWorkflowActivationProperties();
        #endregion

        #region Global variables
        public List<SPWorkflowTaskProperties> taskPropertiesList;
        #endregion

        #region Workflow Activity Handlers
        private void OnWorkflowActivated(object sender, ExternalDataEventArgs e)
        {
            //Check for association data
            if (WorkflowProperties.AssociationData != null)
            {
                /**** Get your association data to be used in craetion of the tasks ****/

                //AssociationData EmployeeExitAssociationData = null;
                //XmlSerializer serializer;
                //XmlTextReader reader;

                //serializer = new XmlSerializer(typeof(AssociationData));
                //reader = new XmlTextReader(new System.IO.StringReader(WorkflowProperties.AssociationData));
            }
        }

        private void ReplicateWork_ChildInitialized(object sender, ReplicatorChildEventArgs e)
        {
            //Set the child instance data to the TaskProperties property of MossTaskManagerActivity activity.
            MossTaskManagerActivity mossTaskManagerActivity = e.Activity as MossTaskManagerActivity;
            //Set task properties
            mossTaskManagerActivity.TaskProperties = e.InstanceData as SPWorkflowTaskProperties;

            //Do extra initialization for task. For instance you can set task content type based on content type id if you are using custom.
        }

        private void ReplicateWork_ChildCompleted(object sender, ReplicatorChildEventArgs e)
        {

        }

        private void ReplicateWork_Completed(object sender, EventArgs e)
        {

        }

        private void ReplicateWork_Initialized(object sender, EventArgs e)
        {
            //Initialize your IList Collection object
            try
            {
                InitializeReplicator();
            }
            catch (Exception)
            {
                SPWorkflowManager.CancelWorkflow(WorkflowProperties.Workflow);
            }
        }

        private void mossTaskManagerActivity1_OnTaskDeleted(object sender, EventArgs e)
        {

        }

        private void mossTaskManagerActivity1_OnSendNotification(object sender, EventArgs e)
        {
            SPWorkflowTaskProperties taskProps = (e as NotificationEventArgs).TaskProperties;
            (e as NotificationEventArgs).Body = "Email body";
        }

        private void mossTaskManagerActivity1_IsTaskCompleted(object sender, EventArgs e)
        {
            SPWorkflowTaskProperties before = (e as TaskEventArgs).BeforeProperties;
            SPWorkflowTaskProperties after = (e as TaskEventArgs).AfterProperties;

            //Compare the before and after props and if task is completed, set the result to true.
            (e as TaskEventArgs).Result = true;
        }
        #endregion

        #region Helper Methods
        private void InitializeReplicator()
        {
            //Important: The SendEmail activity within the MossTaskManagerActivity uses a custom field "Notification". 
            //You may edit/change the field name or create a WDP to be set within the workflow instead of hard coding.

            //Create Task Properties Collection for Replicator

            SPWorkflowTaskProperties taskProperties = new SPWorkflowTaskProperties();
            //taskProperties.AssignedTo = assignedTo; //Assign the task to someone based on your logic
            taskProperties.TaskType = 1;
            taskProperties.Title = String.Format("Complete the task for {0}", WorkflowProperties.Originator);
            taskProperties.PercentComplete = 0.0f;
            taskProperties.SendEmailNotification = false;
            taskProperties.StartDate = DateTime.Now;
            taskProperties.DueDate = DateTime.Now.Add(TimeSpan.FromDays(3));

            //initialize any custom properties based on your custom content type

            //Add task properties to list collection
            this.taskPropertiesList.Add(taskProperties);

            //Initialize and Add more task properties to list collection object.
        }
        #endregion
    }
}
